from sqlalchemy import PrimaryKeyConstraint, Sequence
from sqlalchemy.dialects.postgresql import TIMESTAMP

from tzq.extensions import db


class LookupClassify(db.Model):
    __tablename__ = 'dm_lookup_classify_t'
    __table_args__ = (
        PrimaryKeyConstraint('lookup_classify_id', name='dm_lookup_classify_pk', comment='主键约束'),
        {'comment': 'lookup表-编码表'},
    )
    lookup_classify_id = db.Column(
        db.BigInteger,
        Sequence('dm_lookup_classify_s'),
        primary_key=True,
        server_default=Sequence('dm_lookup_classify_s').next_value(),
        nullable=False,
        comment='主键ID'
    )
    # 业务字段
    classify_code = db.Column(db.String(2000), nullable=False, comment='类别编码')
    classify_name = db.Column(db.String(2000), nullable=False, comment='类别名称')
    classify_desc = db.Column(db.String(2000), comment='类别描述')
    # 审计字段
    principal = db.Column(db.String(100), nullable=False, comment='责任人：HW工号')
    online_date = db.Column(TIMESTAMP(timezone=False, precision=0), nullable=False,
                            server_default=db.func.current_date(), comment='上线时间')
    comment = db.Column(db.String(3000), comment='备注')
    delete_flag = db.Column(db.String(10), nullable=False, server_default='N',
                            comment='删除标识：记录该记录是否删除，Y=>删除、N=>未删除')
    created_by = db.Column(db.String(50), nullable=False, server_default='0.0.0.0',
                           comment='创建人：创建人的客户端IP地址')
    creation_date = db.Column(TIMESTAMP(timezone=False, precision=0), nullable=False,
                              server_default=db.func.clock_timestamp(), comment='创建时间')
    last_updated_by = db.Column(db.String(50), nullable=False, server_default='0.0.0.0',
                                comment='最后更新人：更新人的客户端IP地址')
    last_update_date = db.Column(TIMESTAMP(timezone=False, precision=0), nullable=False,
                                 server_default=db.func.clock_timestamp(), comment='最后更新时间')

    def to_dict(self):
        return {
            'lookup_classify_id': self.lookup_classify_id,
            # 业务字段
            'classify_code': self.classify_code,
            'classify_name': self.classify_name,
            'classify_desc': self.classify_desc,

            # 审计字段
            'principal': self.principal,
            'online_date': self.online_date.strftime('%Y-%m-%d'),
            'comment': self.comment,
            'delete_flag': self.delete_flag,
            'created_by': self.created_by,
            'creation_date': self.creation_date.strftime('%Y-%m-%d %H:%M:%S'),
            'last_updated_by': self.last_updated_by,
            'last_update_date': self.last_update_date.strftime('%Y-%m-%d %H:%M:%S'),
        }


class LookupItem(db.Model):
    __tablename__ = 'dm_lookup_item_t'
    __table_args__ = (
        PrimaryKeyConstraint('lookup_item_id', name='dm_lookup_item_pk', comment='主键约束'),
        {'comment': 'lookup表-项表'},
    )
    lookup_item_id = db.Column(
        db.BigInteger,
        Sequence('dm_lookup_item_s'),
        primary_key=True,
        server_default=Sequence('dm_lookup_item_s').next_value(),
        nullable=False,
        comment='主键ID'
    )
    # 业务字段
    classify_code = db.Column(db.String(2000), nullable=False, comment='类别编码')
    item_code = db.Column(db.String(2000), nullable=False, comment='项编码')
    item_name = db.Column(db.String(2000), nullable=False, comment='名称')
    desc = db.Column(db.String(2000), comment='描述')
    language = db.Column(db.String(2000), nullable=False, comment='语种')
    sort = db.Column(db.String(2000), nullable=False, comment='排序号')
    # 审计字段
    principal = db.Column(db.String(100), nullable=False, comment='责任人：HW工号')
    online_date = db.Column(TIMESTAMP(timezone=False, precision=0), nullable=False,
                            server_default=db.func.current_date(), comment='上线时间')
    comment = db.Column(db.String(3000), comment='备注')
    delete_flag = db.Column(db.String(10), nullable=False, server_default='N',
                            comment='删除标识：记录该记录是否删除，Y=>删除、N=>未删除')
    created_by = db.Column(db.String(50), nullable=False, server_default='0.0.0.0',
                           comment='创建人：创建人的客户端IP地址')
    creation_date = db.Column(TIMESTAMP(timezone=False, precision=0), nullable=False,
                              server_default=db.func.clock_timestamp(), comment='创建时间')
    last_updated_by = db.Column(db.String(50), nullable=False, server_default='0.0.0.0',
                                comment='最后更新人：更新人的客户端IP地址')
    last_update_date = db.Column(TIMESTAMP(timezone=False, precision=0), nullable=False,
                                 server_default=db.func.clock_timestamp(), comment='最后更新时间')

    def to_dict(self):
        return {
            'lookup_item_id': self.lookup_item_id,
            # 业务字段
            'classify_code': self.classify_code,
            'item_code': self.item_code,
            'item_name': self.item_name,
            'desc': self.desc,
            'language': self.language,
            'sort': self.sort,
            # 审计字段
            'principal': self.principal,
            'online_date': self.online_date.strftime('%Y-%m-%d'),
            'comment': self.comment,
            'delete_flag': self.delete_flag,
            'created_by': self.created_by,
            'creation_date': self.creation_date.strftime('%Y-%m-%d %H:%M:%S'),
            'last_updated_by': self.last_updated_by,
            'last_update_date': self.last_update_date.strftime('%Y-%m-%d %H:%M:%S'),
        }


class LookupDelete(db.Model):
    __tablename__ = 'dm_lookup_delete_t'
    __table_args__ = (
        PrimaryKeyConstraint('lookup_delete_id', name='dm_lookup_delete_pk', comment='主键约束'),
        {'comment': 'lookup表-手动删除表'},
    )
    lookup_delete_id = db.Column(
        db.BigInteger,
        Sequence('dm_lookup_delete_s'),
        primary_key=True,
        server_default=Sequence('dm_lookup_delete_s').next_value(),
        nullable=False,
        comment='主键ID'
    )
    # 业务字段
    classify_code = db.Column(db.String(2000), nullable=False, comment='类别编码')
    item_code = db.Column(db.String(2000), comment='项编码')
    # 审计字段
    principal = db.Column(db.String(100), nullable=False, comment='责任人：HW工号')
    online_date = db.Column(TIMESTAMP(timezone=False, precision=0), nullable=False,
                            server_default=db.func.current_date(), comment='上线时间')
    comment = db.Column(db.String(3000), comment='备注')
    delete_flag = db.Column(db.String(10), nullable=False, server_default='N',
                            comment='删除标识：记录该记录是否删除，Y=>删除、N=>未删除')
    created_by = db.Column(db.String(50), nullable=False, server_default='0.0.0.0',
                           comment='创建人：创建人的客户端IP地址')
    creation_date = db.Column(TIMESTAMP(timezone=False, precision=0), nullable=False,
                              server_default=db.func.clock_timestamp(), comment='创建时间')
    last_updated_by = db.Column(db.String(50), nullable=False, server_default='0.0.0.0',
                                comment='最后更新人：更新人的客户端IP地址')
    last_update_date = db.Column(TIMESTAMP(timezone=False, precision=0), nullable=False,
                                 server_default=db.func.clock_timestamp(), comment='最后更新时间')

    def to_dict(self):
        return {
            'lookup_delete_id': self.lookup_delete_id,
            # 业务字段
            'classify_code': self.classify_code,
            'item_code': self.item_code,
            # 审计字段
            'principal': self.principal,
            'online_date': self.online_date.strftime('%Y-%m-%d'),
            'comment': self.comment,
            'delete_flag': self.delete_flag,
            'created_by': self.created_by,
            'creation_date': self.creation_date.strftime('%Y-%m-%d %H:%M:%S'),
            'last_updated_by': self.last_updated_by,
            'last_update_date': self.last_update_date.strftime('%Y-%m-%d %H:%M:%S'),
        }


class LookupDisable(db.Model):
    __tablename__ = 'dm_lookup_disable_t'
    __table_args__ = (
        PrimaryKeyConstraint('lookup_disable_id', name='dm_lookup_disable_pk', comment='主键约束'),
        {'comment': 'lookup表-手动失效表'},
    )
    lookup_disable_id = db.Column(
        db.BigInteger,
        Sequence('dm_lookup_disable_s'),
        primary_key=True,
        server_default=Sequence('dm_lookup_disable_s').next_value(),
        nullable=False,
        comment='主键ID'
    )
    # 业务字段
    classify_code = db.Column(db.String(2000), nullable=False, comment='类别编码')
    item_code = db.Column(db.String(2000), comment='项编码')
    # 审计字段
    principal = db.Column(db.String(100), nullable=False, comment='责任人：HW工号')
    online_date = db.Column(TIMESTAMP(timezone=False, precision=0), nullable=False,
                            server_default=db.func.current_date(), comment='上线时间')
    comment = db.Column(db.String(3000), comment='备注')
    delete_flag = db.Column(db.String(10), nullable=False, server_default='N',
                            comment='删除标识：记录该记录是否删除，Y=>删除、N=>未删除')
    created_by = db.Column(db.String(50), nullable=False, server_default='0.0.0.0',
                           comment='创建人：创建人的客户端IP地址')
    creation_date = db.Column(TIMESTAMP(timezone=False, precision=0), nullable=False,
                              server_default=db.func.clock_timestamp(), comment='创建时间')
    last_updated_by = db.Column(db.String(50), nullable=False, server_default='0.0.0.0',
                                comment='最后更新人：更新人的客户端IP地址')
    last_update_date = db.Column(TIMESTAMP(timezone=False, precision=0), nullable=False,
                                 server_default=db.func.clock_timestamp(), comment='最后更新时间')

    def to_dict(self):
        return {
            'lookup_disable_id': self.lookup_disable_id,
            # 业务字段
            'classify_code': self.classify_code,
            'item_code': self.item_code,
            # 审计字段
            'principal': self.principal,
            'online_date': self.online_date.strftime('%Y-%m-%d'),
            'comment': self.comment,
            'delete_flag': self.delete_flag,
            'created_by': self.created_by,
            'creation_date': self.creation_date.strftime('%Y-%m-%d %H:%M:%S'),
            'last_updated_by': self.last_updated_by,
            'last_update_date': self.last_update_date.strftime('%Y-%m-%d %H:%M:%S'),
        }
